package com.example.plane.GameView;

import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.plane.GameActivity;
import com.example.plane.peng.peng;
import com.example.plane.spirit.Enemy;
import com.example.plane.spirit.Heroplane;

import java.util.ArrayList;


public class GameView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    public Canvas canvas ;
    public SurfaceHolder Holder;
    public  Thread th=new Thread(this);
    public  static  boolean state_pasue=false;
    private long  run_start_time;
    private long  run_end_time;
    private int   e_end_start;
    public static boolean surfaceAvailable;
    public background  ba=new background();
    public Heroplane   heroplane=new Heroplane();
    public Enemy enemy=new Enemy();
    public peng  p=new peng();
    public GameView(Context context)
    {
        super(context);
        initView();
    }
    public void  initView()
    {
        Holder=getHolder();
        Holder.addCallback(this);
        ba.initView();
        heroplane.initView();;
        enemy.initView();
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);
    }

    public void Draw()
    {
      canvas = Holder.lockCanvas();//获取该view下的画布
        ba.doDraw(canvas);
        heroplane.Draw(canvas);
        enemy.Draw(canvas);
      Holder.unlockCanvasAndPost(canvas);

    }
    public void surfaceCreated(SurfaceHolder holder) {
        surfaceAvailable = true;
//        if(null == th){
//            th = new Thread(this);
//        }
                th.start();// 启动线程


    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
     surfaceAvailable = false;

    }
    public void logic()
    {
    ba.bglogic();
    heroplane.logic();
    enemy.logic();
    }
    public void penglistener(){
        for(int i=0;i<enemy.enmey_bitmaps.size();i++) {
            for(int j=0;j<heroplane.hpbuttle.the_buttle.size();j++) {
                p.penginit(heroplane.hpbuttle.the_buttle.get(j), heroplane.hpbuttle.buttlex.get(j), heroplane.hpbuttle.buttley.get(j), enemy.enmey_bitmaps.get(i), enemy.ex.get(i), enemy.ey.get(i));
           if(p.Jpeng()) {
               ;enemy.enmey_bitmaps.remove(i);
               enemy.ey.remove(i);
               enemy.ex.remove(i);
               heroplane.hpbuttle.the_buttle.remove(i);
               heroplane.hpbuttle.buttlex.remove(i);
               heroplane.hpbuttle.buttley.remove(i);
           }
           };
        }
    }
    public void run()
    {
        while(surfaceAvailable) {
            try {
                run_start_time = System.currentTimeMillis();
                if(state_pasue==true)//是否暂停
                {}
                else {
                    Draw();
                    logic();
                    penglistener();
                    run_end_time = System.currentTimeMillis();
                    e_end_start = (int) (run_end_time - run_start_time);
                    try {
                        if (e_end_start > 1000 / 60) {
                        } else {
                            e_end_start = (int) (System.currentTimeMillis() - run_end_time);
                            Thread.sleep(1000 / 60 - e_end_start);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }catch (Exception e){}
        }
    }

}


